<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="name" label="名称">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <ren-select v-model="dataForm.status" dict-type="member_level_status" placeholder="状态"></ren-select>
      </el-form-item>
      <el-form-item label="标签图标" prop="paramJsonObj.icon">
        <el-input v-model="dataForm.paramJsonObj.icon" placeholder="会员旗图标">
          <template #suffix>
            <svg class="iconfont" aria-hidden="true" :color="dataForm.value">
              <use :xlink:href="`#${dataForm.paramJsonObj.icon}`"></use>
            </svg>
          </template>
        </el-input>
        <div class="mod-sys__menu-icon-inner">
          <div class="mod-sys__menu-icon-list">
            <el-button v-for="(item, index) in iconList" :key="index" @click="iconListCurrentChangeHandle(item)" :class="{ 'is-active': dataForm.type === item }">
              <svg class="icon-svg" aria-hidden="true"><use :xlink:href="`#${item}`"></use></svg>
            </el-button>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="标签颜色" prop="paramJsonObj.color">
        <el-color-picker v-model="dataForm.paramJsonObj.color" show-alpha :predefine="predefineColors"> </el-color-picker>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { getIconList } from "@/utils/utils";
import baseService from "@/service/baseService";

import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  status: 1,
  type: "",
  paramJsonObj: {
    icon: "",
    color: "rgba(0, 206, 209, 1)"
  },
  value: ""
});

const iconList = ref<string[]>([]);
const predefineColors = ref(["#ff4500", "#ff8c00", "#ffd700", "#90ee90", "#00ced1", "#1e90ff", "#c71585", "rgba(255, 69, 0, 0.68)", "rgb(255, 120, 0)", "hsv(51, 100, 98)", "hsva(120, 40, 94, 0.5)", "hsl(181, 100%, 37%)", "hsla(209, 100%, 56%, 0.73)"]);

const rules = ref({
  name: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  "paramJsonObj.icon": [{ required: true, message: t("validate.required"), trigger: ["blur", "change"] }],
  "paramJsonObj.color": [{ required: true, message: t("validate.required"), trigger: "blur" }],
  status: [{ required: true, message: t("validate.required"), trigger: "blur" }]
});

const init = (row?: any) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  iconList.value = getIconList();

  if (row) {
    Object.assign(dataForm, row);
  }
};

// 图标, 选中
const iconListCurrentChangeHandle = (icon: string) => {
  dataForm.paramJsonObj.icon = icon;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/member/flag", dataForm, {}).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};
defineExpose({
  init
});
</script>
<style lang="scss" scoped>
.mod-sys__menu-icon-list {
  width: 100%;
  margin-top: 10px;
  height: 200px;
  overflow: auto;
  :deep(.el-button) {
    margin-left: 12px;
  }
  .icon-svg {
    font-size: 22px;
  }
}
</style>
